#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#include "shop_data.au3"
#include "shop_proc.au3"

Func parse_input_shop($pos)
	Local $str = input($pos)
	If $str == "" Then
		Local $ret[1][1] = [[0]]
		Return $ret
	EndIf
	Local $ret = StringRegExp($str,"\b(\d+),(\d+)\b",3)
	If @error = 0 Then
		Local $r[UBound($ret)/2 + 1][2]
		$r[0][0] = UBound($ret)/2
		For $i = 0 To UBound($ret) - 1 Step 2
			$r[$i/2+1][0] = $ret[$i]
			$r[$i/2+1][1] = $ret[$i+1]
			_DebugOut("search item: " & $ret[$i] & "-" & $ret[$i+1])
		Next
		Return $r
	ElseIf @error = 2 Then
		_DebugOut("bad regexp against line2 '" & $str & "'")
	Else
		_DebugOut("bad line '" & $str & "'")
	EndIf
EndFunc

Global Const $shop_stage_1 = 20
Global Const $shop_stage_2 = 30
Global Const $shop_stage_3 = $shop_stage_2 + 10
Global Const $shop_stage_end = 1000
Global $shop_img = 10000

Global $test = 0

Func search_item($pos)
	Switch $substep[$pos]
		Case 0
			;_DebugOut("try open")
			If open_shop($pos) Then
				$substep[$pos] = 1
				next_over($pos,500)
			Else
				_DebugOut("no shop")
				Return False
			EndIf
		Case 1 To $shop_stage_1
			get_shop_data($pos)
			;get_shop_data($pos,"..\..\data\shop-" & $shop_img & ".bmp")
			If shop_opened($pos) Then
				;_DebugOut("shop is open ****************************")
				$shop_img += 1
				$substep[$pos] = $shop_stage_1 + 1
				$test = 0
			ElseIf $substep[$pos] < $shop_stage_1 Then
				$substep[$pos] += 1
			Else
				$test += 1
				$substep[$pos] = 0
			EndIf
		Case $shop_stage_1 + 1
			If check_shop_info($pos) Then
				_DebugOut("shop ready ***********************")
				$substep[$pos] = $shop_stage_1 + 2
			Else
				;_DebugOut("info incomplete")
				next_over($pos,200)
				$substep[$pos] = 1
				$shop_img -= 1
			EndIf
		Case $shop_stage_1 + 2
			make_progress($pos)
			store_shop_info($pos)
			$stepdata[$pos] = parse_input_shop($pos)
			$substep[$pos] = $shop_stage_2
			_DebugOut("shop complete ------------------------")
		Case $shop_stage_2 To $shop_stage_3 - 1
			_DebugOut("shop check -- " & $substep[$pos])
			Local $place = $substep[$pos] - $shop_stage_2
			While shop_empty_place($pos, $place) And $substep[$pos] < $shop_stage_3
				$substep[$pos] += 1
				$place = $substep[$pos] - $shop_stage_2
			WEnd
			;If Not shop_empty_place($pos, $place) Then
			If $substep[$pos] < $shop_stage_3 Then
				Local $has = shop_has_item($pos, $place, $stepdata[$pos])
				If $has Then
					StopWork($pos,"find(" & $place & ") " & $has)
					_DebugOut("shop " & "find(" & $place & ") " & $has)
					;$substep[$pos] = $shop_stage_end
				Else
					$substep[$pos] += 1
				EndIf
			EndIf
		Case $shop_stage_3
			_DebugOut("shop done ++++++++++++++++++++")
			If False Then
				$substep[$pos] = $shop_stage_end
			Else
				close_shop($pos)
				next_over($pos,200)
				Return False
			EndIf
		Case $shop_stage_end
			StopWork($pos,"all done")
			;Beep()
	EndSwitch
	Return True ; need continue work
EndFunc
